--[[

LysKitty

This addon is an RP-centric addon.

It is written to enable a person to type a special message in /say, /guild, /officer
that is sent out "normally" in a non-standard language form,
that can be understood by others running the addon.

This allows special RP chatter that is not currently possible within the current
World of Warcraft chat system.

As far as I know, this addon does not violate the Blizzard addon/chat policies in
existance at this time.  I would be happy to hear from a Blizzard employee should that
be incorrect, or if I need to change the addon in some way to conform.

]]

--[[
Specially handled functions, more generic than addon specific.
]]
-- Function: Slash command handler
function LK_SlashCommandHandler(msg)
	if (msg == '') then
		msg = nil;
	end

	if (msg) then
		msgLower = string.lower(msg);
		-- Handle messages sent to guild
		if (string.find(msgLower, LK_GUILD) ~= nil) then
			local value = EFM_SF_SlashClean(LK_GUILD, msg);
			LK_SendMessage(LK_GUILD, value);
			return;

		-- Handle messages sent to say
		elseif (string.find(msgLower, LK_SAY) ~= nil) then
			local value = EFM_SF_SlashClean(LK_SAY, msg);
			LK_SendMessage(LK_SAY, value);
			return;

		-- Handle messages sent as emotes
		elseif (string.find(msgLower, LK_EMOTE) ~= nil) then
			local value = EFM_SF_SlashClean(LK_EMOTE, msg);
			LK_SendMessage(LK_EMOTE, value);
			return;

		-- Handle messages sent to whispers (not fully functional yet)
		elseif (string.find(msgLower, LK_WHISPER) ~= nil) then
			local value = EFM_SF_SlashClean(LK_WHISPER, msg);
			LK_SendMessage(LK_WHISPER, value);
			return;

		-- Handle test messages
		elseif (string.find(msgLower, LK_TEST) ~= nil) then
			local value = EFM_SF_SlashClean(LK_TEST, msg);
			LK_SendMessage(LK_TEST, value);
			return;
		end
	end

	-- Display help when all else fails...
	DEFAULT_CHAT_FRAME:AddMessage("Displaying "..LK_NAME.." help.");
	local index = 1;
	local value = getglobal("LK_HELP"..index);
	while( value ) do
		DEFAULT_CHAT_FRAME:AddMessage(value);
		index = index + 1;
		value = getglobal("LK_HELP"..index);
	end
end

-- Function: Return the string value for slash command options...
function LKL_SF_SlashClean(commandString, msgLine)
	local tempValue;

	tempValue = string.sub(msgLine, (string.len(commandString) + 2));
	
	if (string.find(tempValue, "^\"") ~= nil) then
		tempValue = string.sub(tempValue, 2);
	end
	
	if (string.find(tempValue, "\"$") ~= nil) then
		tempValue = string.sub(tempValue, 1, (string.len(tempValue) - 1));
	end

	return tempValue;
end

--[[
Addon Specific Code.
]]

-- Function: Do the initial program load stuff.
function LK_OnLoad(frame)
	frame:RegisterEvent("ADDON_LOADED");
	frame:RegisterEvent("CHAT_MSG_ADDON");
end

-- Function: Call this on events.
function LK_OnEvent(frame, event, ...)
	if ((event == "ADDON_LOADED") and (select(1, ...) == "LysKitty")) then
		DEFAULT_CHAT_FRAME:AddMessage(LK_LOADED);

		-- Register our slash commands
		SLASH_LKC1 = LK_SLASH1;
		SLASH_LKC2 = LK_SLASH2;
		SlashCmdList["LKC"] = function(msg)
			LK_SlashCommandHandler(msg);
		end

		-- Set default values for configuration variables.
		LKC_SetConfig();
		
		-- Set up configuration screen
		LKC_RegConfig();
		
		return;

	elseif (event == "CHAT_MSG_ADDON") then
		if (select(1, ...) == LK_ADDON_PREFIX) then
			local addon_msg = select(2, ...);
			
			--DEFAULT_CHAT_FRAME:AddMessage("LKC Debug: "..addon_msg);

			local code, message = strsplit("|", addon_msg);

			if (code ~= "") then
				for index=1,3 do
					if (code == LK_SECRET[index]) then
						DEFAULT_CHAT_FRAME:AddMessage(LK_TRANSLATE..index..": "..message, 0.8, 0.8, 0.8);
					end
				end
			else
				DEFAULT_CHAT_FRAME:AddMessage(LK_TRANSLATE..message, 0.8, 0.8, 0.8);
			end
		end

		return;

	end
end

-- Function: Send the special translated message via the defined chat channel.
function LK_SendMessage(channel, message)

	-- Send Guild Message
	if (channel == LK_GUILD) then
		-- Garble the text to output
		local publicMessage = LK_GarbleMessage(message);

		-- Send Chat Message
		SendChatMessage(LK_PREFIX.." "..publicMessage, "GUILD", nil);

		-- Send Addon Message
		if (LK_SECRET[1]) then
			SendAddonMessage(LK_ADDON_PREFIX, LK_SECRET[1].."|"..message, "GUILD");
		else
			SendAddonMessage(LK_ADDON_PREFIX, "|"..message, "GUILD");
		end
		
	-- Send Say Message
	elseif (channel == LK_SAY) then
		-- Garble the text to output
		local publicMessage = LK_GarbleMessage(message);

		-- Send Chat Message
		SendChatMessage(LK_PREFIX.." "..publicMessage, "SAY", nil);

		-- Send Addon Message
		if (LK_SECRET[1]) then
			SendAddonMessage(LK_ADDON_PREFIX, LK_SECRET[1].."|"..message, "GUILD");
		else
			SendAddonMessage(LK_ADDON_PREFIX, "|"..message, "GUILD");
		end

	-- Send Say Message
	elseif (channel == LK_EMOTE) then
		-- Garble the text to output
		local publicMessage = LK_GarbleMessage(message);

		-- Send Chat Message
		SendChatMessage(LK_EMOTEPREFIX.." \""..publicMessage.."\"", "EMOTE", nil);

		-- Send Addon Message
		if (LK_SECRET[1]) then
			SendAddonMessage(LK_ADDON_PREFIX, LK_SECRET[1].."|"..message, "GUILD");
		else
			SendAddonMessage(LK_ADDON_PREFIX, "|"..message, "GUILD");
		end

	-- Send Whisper Message
	elseif (channel == LK_WHISPER) then
		local target = "testermctesty";
		
		-- Garble the text to output
		local publicMessage = LK_GarbleMessage(message);

		-- Send Chat Message
		SendChatMessage(LK_PREFIX..publicMessage, "WHISPER", nil, target);

		-- Send Addon Message
		if (LK_SECRET[1]) then
			SendAddonMessage(LK_ADDON_PREFIX, LK_SECRET[1].."|"..message, "WHISPER", target);
		else
			SendAddonMessage(LK_ADDON_PREFIX, "|"..message, "WHISPER", target);
		end
		

	-- Send Test Message
	elseif (channel == LK_TEST) then
		-- Garble the text to output
		local publicMessage = LK_GarbleMessage(message);

		-- Send Chat Message
		DEFAULT_CHAT_FRAME:AddMessage("Test Message: "..publicMessage, 0.8, 0.8, 0.8);

		-- Send Addon Message
		if (LK_SECRET[1]) then
			SendAddonMessage(LK_ADDON_PREFIX, LK_SECRET[1].."|"..message, "GUILD");
		else
			SendAddonMessage(LK_ADDON_PREFIX, "|"..message, "GUILD");
		end
	end


end

-- Function: Garble a given text string and return a string using values from a lookup table.
function LK_GarbleMessage(message)
	local splitMessage	= { strsplit(" ", message) };
	local messageTable	= {};

	-- Iterate over each word, randomly substituting from the possible substitutes.
	for i,v in ipairs(splitMessage) do
		local randomNum = random(1, table.getn(LK_SUBS));

		while(LK_SUBS[randomNum] == "") do
			randomNum = random(1, table.getn(LK_SUBS));
		end
		
		table.insert(messageTable, LK_SUBS[randomNum]);
	end

	local newMessage = table.concat(messageTable, " ");
	return newMessage;
end
